package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.workflow.dao.BpmNewFlowTriggerDao;
import com.ccp.dev.workflow.model.BpmNewFlowTrigger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassDescribe:
 * 触发新流程设置业务逻辑层
 * @author :wangcheng
 * Date: 2019-07-12
 */
@Service
public class BpmNewFlowTriggerService extends BaseService<BpmNewFlowTrigger> {
    @Resource
    private BpmNewFlowTriggerDao bpmNewFlowTriggerDao;

    /**
     * 通过 节点ID 流程key获取触发设置
     * @param flowKey  流程key
     * @param nodeId 节点ID
     */
    public BpmNewFlowTrigger getByFlowKeyNodeId(String flowKey, String nodeId) {
        return bpmNewFlowTriggerDao.getByFlowKeyNodeId(flowKey, nodeId);
    }

    /**
     * 根据id数组批量删除数据
     * @param ids id字符串数组
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByIds(String[] ids) {
        for (String id : ids) {
            bpmNewFlowTriggerDao.delById(id);
        }
    }

    /**
     * 根据流程key获取触发定义。
     * @param flowKey 流程key
     * @return 触发新流程列表
     */
    public List<BpmNewFlowTrigger> queryByFlowKey(String flowKey) {
        return bpmNewFlowTriggerDao.queryByFlowKey(flowKey);
    }

    /**
     * 获取某动作触发流程数据
     * @param nodeId 节点id
     * @param flowKey 流程key
     * @param action 动作
     */
    public BpmNewFlowTrigger getByNodeAction(String nodeId, String flowKey, String action) {
        BpmNewFlowTrigger trigger = bpmNewFlowTriggerDao.getByFlowKeyNodeId(flowKey, nodeId);
        if (trigger != null && action.equals(trigger.getAction())) {
            return trigger;
        }
        return null;
    }
}
